﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Keyki.NefClass.Kernel.Fuzzy.FuzzySet
{
	public sealed class GausFuzzySet : AbstractFuzzySet
	{
		private double mean;
		private double var;

		public GausFuzzySet()
			: this(Double.NaN, Double.NaN)
		{ }

		public GausFuzzySet(double mean, double var)
		{
			this.mean = mean;
			this.var = var;
		}//GausFuzzySet

		public override void Init(IEnumerable<double> collection)
		{
			mean = collection.Average();
			var = collection.Variance(mean);
		}//Init

		public override double Compute(double value)
		{
			return Math.Exp(-Math.Pow(value - mean, 2) / (2 * var));
		}

		public override AbstractFuzzySet Clone()
		{
			return new GausFuzzySet(this.mean, this.var);
		}//Compute

		public override bool Equals(object obj)
		{
			if (!(obj is GausFuzzySet))
				return false;

			var fuzzySet = (GausFuzzySet) obj;

			return (fuzzySet.mean == mean && fuzzySet.var == var);
		}//Equals

		public override int GetHashCode()
		{
			return base.GetHashCode();
		}//GetHashCode
	}//GausFuzzySet
}